package Ajax;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;

public class IterPage extends AbsPersonalPage {

	public IterPage(String resumeId, HttpServletRequest request) {
		super(resumeId, request);
		mStrSubClassName = "resumeiter";
	}

	@Override
	public void execSql(Statement stmt, HttpServletRequest request)
			throws SQLException {
		if (bCreate) {
			return;
		}

		if (bSubmit) {
			String sql = "INSERT INTO iter VALUES(null, " + mResumeId + ", "
					+ (request.getParameter("property") == null ? "false" : "true") + ", "
					+ Integer.valueOf(request.getParameter("itera")) + ", "
					+ Integer.valueOf(request.getParameter("iterb")) + ", "
					+ Integer.valueOf(request.getParameter("month")) + ", "
					+ Integer.valueOf(request.getParameter("ableid")) + ")";

			if (nModify > 0) {
				sql = "UPDATE iter SET "
					+ "property = " + (request.getParameter("property") == null ? "false" : "true")
					+ ", itera = " + Integer.valueOf(request.getParameter("itera"))
					+ ", iterb = " + Integer.valueOf(request.getParameter("iterb"))
					+ ", ableid = " + Integer.valueOf(request.getParameter("ableid"))
					+ ", month = " + Integer.valueOf(request.getParameter("month"))
					+ " WHERE id = " + nModify;
			}

			stmt.executeUpdate(sql);

			return;
		}

		if (bDelete) {
			String sql = "DELETE FROM iter WHERE id = " + mId;

			stmt.executeUpdate(sql);

			return;
		}

		String sql = "SELECT iter.id, iter.property, iter.itera, iter.iterb, iter.month, iter.ableid," +
				 "cate_itera.list_name, cate_iterb.list_name, cate_skillable.list_name FROM iter LEFT " +
			 	 "JOIN cate_itera ON iter.itera = cate_itera.id " +
			 	 "JOIN cate_iterb ON iter.iterb = cate_iterb.id " +
				 "JOIN cate_skillable ON iter.ableid = cate_skillable.id WHERE ";

		if (nModify > 0) {
			sql += "iter.id = " + nModify;
		} else {
			sql += "iter.resume_id = " + mResumeId;
		}

		rs = stmt.executeQuery(sql);
	}

	@Override
	protected void writeWithoutCreate(PrintWriter writer) throws SQLException {
		if (rs == null) {
			return;
		}

		writer.print("<ol><li>属性: 公开, 您的简历可以被全部企业用户查看</li><li>属性: 隐藏, 企业用户不可以查看您的简历详情</li>"
				+ "</ol>"
				+ "<div class=\"topic_list\">"
				+ "<table cellpadding=\"0\" cellspacing=\"0\">"
				+ "<thead>"
				+ "<tr><td>属性</td><td>技能名称</td><td>使用时间</td><td>熟练程度</td><td>管理选项</td></tr>"
				+ "</thead>");

		int i = 0;

		while (rs.next()) {
			boolean is_public = rs.getBoolean("iter.property");

			writer.println("<tr class=\"bg" + (++i % 2) + "\">");
			writer.println("<td><img src=\"./images/personal/property_"
					+ (is_public ? "y" : "n") + ".gif\" alt=\"属性: "
					+ (is_public ? "公开" : "隐藏") + "\" /></td>");

			writer.println("<td>" + rs.getString("cate_iterb.list_name") + "</td>");
			writer.println("<td>" + rs.getInt("iter.month") + " (月)</td>");
			writer.println("<td>" + rs.getString("cate_skillable.list_name") + "</td>");
			writer.println("<td><a href=\"#\" onclick=\"resumeiter('','','"
					+ rs.getString("id") + "');return false;\">修改</a> | ");
			writer.println("<a href=\"#\" onclick=\"if (confirm('您是否确定要删除该条IT技能记录\t')){resumeiter('','','','"
					+ rs.getString("id")
					+ "');return false;}else{return false;}\">删除</a></td>");
		}

		writer.println("</tr></table></div><table cellpadding=\"0\" cellspacing=\"0\" class=\"formtable\"><tr><th width=\"100\">&nbsp;</th>	<td>");
		writer.println("<input type=\"button\" name=\"create\" value=\"添加IT技能\" onclick=\"resumeiter('',true)\" class=\"submit\" />");
		writer.println("<input type=\"button\" name=\"nextstep\" value=\"跳到下一步\" onclick=\"resumeload('program')\" class=\"button\" />");
		writer.println("</td></tr></table><div id=\"resumextentform\"></div>");
	}

	@Override
	protected void writeWithCreate(PrintWriter writer) throws IOException {
		String form_title = "<form action=\"ResumeSys?action=personal&amp;option=resume&amp;extent=iter&amp;info=" + mResumeId + "&amp;submit=1\" ";
		File file = new File("c:\\xml\\iter.xml");

		if (file.exists()) {
			InputStream fs = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fs,
					"utf-8"));

			String Line = null;

			writer.print(form_title);

			while ((Line = br.readLine()) != null) {
				writer.println(Line);
			}

			br.close();
			fs.close();
		} else {
			file.createNewFile();
		}
	}

	@Override
	protected void writeModify(PrintWriter writer) throws SQLException {
		if (rs == null || !rs.next()) {
			return;
		}

		boolean is_public = rs.getBoolean("iter.property");

		writer.print("<form action=\"ResumeSys?action=personal&amp;option=resume&amp;extent=iter&amp;info=" + mResumeId + "&amp;submit=1\" ");
		writer.print("name=\"wane_resume\" method=\"post\" target=\"ajaxform\" onsubmit=\"return resumeiter();\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"formtable\">");
		writer.print("<caption><h2>IT技能设置 - 修改 - " + rs.getString("cate_iterb.list_name") + "</h2></caption><tr><th width=\"100\"><span class=\"warn\">*</span> 技能名称:</th><td>");
		writer.print("<a href=\"#\" onclick=\"category.load('titera','itera',document.wane_resume.itera.value);return false;\" class=\"select\">");
		writer.print("<span id=\"titera\">" + rs.getString("cate_itera.list_name") + "</span></a><input type=\"hidden\" id=\"itera\" name=\"itera\" value=\"" + rs.getInt("iter.itera") + "\" />");
		writer.print("<input type=\"hidden\" id=\"ditera\" name=\"ditera\" value=\"\" />");
		writer.print("<a href=\"#\" onclick=\"category.load('titerb','iterb',document.wane_resume.itera.value);return false;\" class=\"select\">");
		writer.print("<span id=\"titerb\">" + rs.getString("cate_iterb.list_name") + "</span></a>");
		writer.print("<input type=\"hidden\" id=\"iterb\" name=\"iterb\" value=\"" + rs.getInt("iter.iterb") + "\" />");
		writer.print("<input type=\"hidden\" id=\"diterb\" name=\"diterb\" value=\"\" />");
		writer.print("</td></tr><tr><th><span class=\"warn\">*</span> 属性:</th><td>");
		writer.print("<input type=\"checkbox\" name=\"property\" value=\"1\" ");

		if (is_public) {
			writer.print("checked");
		}

		writer.print(" /> 属性: 公开</td></tr><tr><th><span class=\"warn\">*</span> 使用时间:</th><td><input type=\"text\" name=\"month\" class=\"t_input\" size=\"4\" value=\"");
		writer.print(rs.getInt("iter.month") + "\" /> (月)</td></tr>");

		writer.print("<tr><th><span class=\"warn\">*</span> 熟练程度:</th><td><a href=\"#\" onclick=\"category.load('tableid','skillable',document.wane_resume.ableid.value);return false;\" class=\"select\">");
		writer.print("<span id=\"tableid\">" + rs.getString("cate_skillable.list_name") + "</span></a>");
		writer.print("<input type=\"hidden\" id=\"ableid\" name=\"ableid\" value=\"" + rs.getInt("iter.ableid") + "\" />");
		writer.print("<input type=\"hidden\" id=\"dableid\" name=\"dableid\" value=\"\" /><input type=\"hidden\" name=\"modifysubmit\" value=\"1\" />");
		writer.print("<input type=\"hidden\" name=\"modifid\" value=\"" + nModify + "\">");
		writer.print("</td></tr><tr><th>&nbsp;</th><td><input type=\"submit\" name=\"Submit\" value=\"保存修改\" class=\"submit\" /><input type=\"button\" name=\"ClickBack\" value=\"取消操作\" onclick=\"resumeload('iter');\" class=\"button\" /></td></tr></table></form>");
	}
}
